package com.example.littleken.ormlite_text.Activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.littleken.ormlite_text.R;
import com.example.littleken.ormlite_text.Utils.Utils;
import com.example.littleken.ormlite_text.bean.LifeDiary;
import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;

/**
 * 删除页面Activity
 */
public class DeleteActivity extends AppCompatActivity implements View.OnClickListener{
    private Button btn_update_for_delete;
    private Button btn_goback_for_delete;
    private EditText et_title_delete;
    private EditText et_lifediary_delete;
    //用于数据库连接
   /* public DatabaseHelper databaseHelper = null;*/
    public Dao lifediaryDAO = null;
    //用于接收跳转过来时携带的信息
    private Intent intent = null;
    private Bundle bundle = null;
    public static final String BUNDLE_MAIN_TO_UPDATE = "bundleformaintoupdate";
    //用于保存对应实体类
    private LifeDiary lifediary = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete);
        //取得传递过来的信息
        intent = getIntent();
        bundle = intent.getExtras();
        //取得对应实体类
        lifediary = (LifeDiary) bundle.getSerializable(BUNDLE_MAIN_TO_UPDATE);

        /*initDb();//初始化数据库连接*/
        lifediaryDAO= Utils.initDb(DeleteActivity.this);
        initView();
    }

    private void initView() {
        btn_update_for_delete = (Button) findViewById(R.id.btn_update_for_delete);
        btn_goback_for_delete = (Button) findViewById(R.id.btn_goback_for_delete);
        et_title_delete = (EditText) findViewById(R.id.et_title_delete);
        et_lifediary_delete = (EditText) findViewById(R.id.et_lifediary_delete);
        btn_update_for_delete.setOnClickListener(this);
        btn_goback_for_delete.setOnClickListener(this);
        et_title_delete.setText(lifediary.getTitle());
        et_lifediary_delete.setText(lifediary.getMessage());
    }

/*    public void initDb() {//初始化数据库
        try {
            databaseHelper = DatabaseHelper.getInstance(DeleteActivity.this);
            lifediaryDAO = databaseHelper.getDao(LifeDiary.class);
        } catch (SQLException e) {
            Log.e("ERROR","DeleteActivity数据库getDao拉闸");
            e.printStackTrace();
        }
    }*/

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_update_for_delete:
                delete(lifediary,DeleteActivity.this);//删除日记
                this.setResult(RESULT_OK);//成功删除
                break;
            case R.id.btn_goback_for_delete:
                this.setResult(RESULT_CANCELED);//取消删除
                Toast.makeText(DeleteActivity.this,(String)this.getResources().getText(R.string.notsure_delete),Toast.LENGTH_SHORT).show();
                this.finish();
                break;
        }
    }

    /**
     * 用于删除日记
     * @param lifeDiary 需要删除的日记实体类
     * @param context 上下文
     */
    public void delete(final LifeDiary lifeDiary, final Context context){
       /* final boolean[] flag = {false};*/
       //生成询问删除dialog，防止误触
        final AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle((String) this.getResources().getText(R.string.deletediary))
                .setMessage((String) this.getResources().getText(R.string.askfordeletediary))
                .setNegativeButton((String) this.getResources().getText(R.string.no),null)
                .setPositiveButton((String) this.getResources().getText(R.string.yes), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        try {
                            //删除日记
                            lifediaryDAO.deleteById(lifeDiary.getId());
                            Toast.makeText(context,context.getResources().getText(R.string.deletesuccess),Toast.LENGTH_SHORT).show();
                            //接收本Activity，跳转回MainActivity
                            AppCompatActivity a=(AppCompatActivity) context;
                            a.finish();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }).show();
    }
}
